/*
 * @Author: moeWang
 * @Date: 2022-03-07 13:45:00
 * @LastEditTime: 2022-03-08 09:36:51
 * @LastEditors: moeWang
 * @Description:
 * @FilePath: \nest-web\src\logical\role\entity\role.entity.ts
 */

import { ApiPropertyOptional } from '@nestjs/swagger';
import {
  AutoIncrement,
  BelongsToMany,
  Column,
  Comment,
  DataType,
  ForeignKey,
  HasMany,
  Model,
  PrimaryKey,
  Table,
} from 'sequelize-typescript';
import { DataTypes } from 'sequelize';
import { RpAccount } from 'src/logical/account/entity/account.entity';
import { MenuEntity } from 'src/logical/menu/entity/menu.entity';
import { Col } from 'sequelize/types/utils';

@Table({
  tableName: 'rp_role',
  timestamps: false,
  comment: '角色表',
})
export class RoleEntity extends Model<RoleEntity> {
  @ApiPropertyOptional({ description: '主键' })
  @Comment('id')
  @PrimaryKey
  @Column({
    type: DataTypes.BIGINT,
    field: 'role_id',
    allowNull: false,
    primaryKey: true,
    autoIncrement: true,
  })
  roleId: number;

  @ApiPropertyOptional({ description: '角色名' })
  @Comment('角色名')
  @Column({
    type: DataTypes.CHAR,
    field: 'role_name',
  })
  roleName: string;

  @ApiPropertyOptional({ description: '描述' })
  @Comment('描述')
  @Column({
    type: DataTypes.CHAR,
  })
  description: string;

  @BelongsToMany(() => RpAccount, () => RoleAccountEntity)
  accounts: RpAccount[];

  @BelongsToMany(() => MenuEntity, () => RoleMenuEntity)
  menus: MenuEntity[];
}

@Table({
  tableName: 'rp_role_account',
  timestamps: false,
})
export class RoleAccountEntity extends Model<RoleAccountEntity> {
  @ForeignKey(() => RoleEntity)
  @Column({
    field: 'role_id',
  })
  roleId: string;
  @ForeignKey(() => RpAccount)
  @Column({
    field: 'account_id',
  })
  accountId: string;
}

@Table({
  tableName: 'rp_role_menu',
  timestamps: false,
})
export class RoleMenuEntity extends Model<RoleMenuEntity> {
  @ForeignKey(() => RoleEntity)
  @Column({
    field: 'role_id',
  })
  roleId: string;
  @ForeignKey(() => MenuEntity)
  @Column({
    field: 'menu_id',
  })
  menuId: string;
}
